package com.bincloud.vue.admin.web.service.ums.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bincloud.vue.admin.common.enums.ums.UmsResourceStateEnum;
import com.bincloud.vue.admin.common.enums.ums.UmsResourceTypeEnum;
import com.bincloud.vue.admin.common.exception.BaseException;
import com.bincloud.vue.admin.common.exception.CodeMsg;
import com.bincloud.vue.admin.common.model.dto.response.BasePageResDTO;
import com.bincloud.vue.admin.common.util.BaseEntityUtils;
import com.bincloud.vue.admin.common.util.ObjectUtils;
import com.bincloud.vue.admin.dao.mapper.ums.UmsResourceMapper;
import com.bincloud.vue.admin.model.entity.UmsResource;
import com.bincloud.vue.admin.web.dto.ums.resource.*;
import com.bincloud.vue.admin.web.service.ums.IUmsResourceService;
import com.bincloud.vue.admin.web.util.request.RequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Service
@Slf4j
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements IUmsResourceService {
    @Resource
    private RequestService requestService;

    /**
     * 获取资源信息
     *
     * @param code 入参
     * @return 返参
     */
    @Override
    public UmsResource getResource(String code) {
        return this.getOne(Wrappers.<UmsResource>lambdaQuery().eq(UmsResource::getCode, code));
    }

    /**
     * 资源分页查询
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public BasePageResDTO<List<UmsResourcePageResDTO>> pageResource(UmsResourcePageReqDTO reqDTO) {
        // 分页查询入参
        QueryWrapper<UmsResource> queryWrapper = new QueryWrapper<>();
        BeanUtils.copyProperties(reqDTO, queryWrapper);
        Page<UmsResource> queryPage = new Page<>(reqDTO.getPageNo(), reqDTO.getPageSize());
        Page<UmsResource> resPage = this.page(queryPage, queryWrapper);
        // 组装参数
        List<UmsResourcePageResDTO> resList = resPage.getRecords().stream().map(source -> {
            UmsResourcePageResDTO target = new UmsResourcePageResDTO();
            BeanUtils.copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());
        // 组装返参
        BasePageResDTO<List<UmsResourcePageResDTO>> resDTO = new BasePageResDTO<>();
        resDTO.setPageNo(resPage.getCurrent());
        resDTO.setPageSize(resPage.getSize());
        resDTO.setTotalPage(resPage.getPages());
        resDTO.setTotal(resPage.getTotal());
        resDTO.setContent(resList);
        return resDTO;
    }

    /**
     * 资源新增
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean saveResource(UmsResourceSaveReqDTO reqDTO) {
        // 验证入参
        this.validateUmsResourceSaveReqDTO(reqDTO);
        // 组装参数
        UmsResource resource = new UmsResource();
        BeanUtils.copyProperties(reqDTO, resource);
        BaseEntityUtils.create(resource, requestService.getUsername());
        return this.save(resource);
    }

    /**
     * 资源删除
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean removeResource(UmsResourceReqDTO reqDTO) {
        UmsResource resource = this.getById(reqDTO.getId());
        if (ObjectUtils.isEmpty(resource)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_NOT_EXIST);
        }
        // 删除数据
        return this.removeById(resource);
    }

    /**
     * 资源更新
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean modifyResource(UmsResourceModifyReqDTO reqDTO) {
        UmsResource resource = this.getById(reqDTO.getId());
        if (ObjectUtils.isEmpty(resource)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_NOT_EXIST);
        }
        // 验证入参
        this.validateUmsResourceModifyReqDTO(resource, reqDTO);
        // 组装参数
        BeanUtils.copyProperties(reqDTO, resource);
        BaseEntityUtils.create(resource, requestService.getUsername());
        return this.updateById(resource);
    }

    private void validateUmsResourceModifyReqDTO(UmsResource resource, UmsResourceModifyReqDTO reqDTO) {
        // 资源是否重复
        if (ObjectUtils.isNotEmpty(resource) && ObjectUtils.equals(resource.getId(), reqDTO.getId())) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_EXISTED);
        }
        // 验证类型
        if (ObjectUtils.isEmpty(reqDTO.getType()) || UmsResourceTypeEnum.noCode(reqDTO.getType())) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_TYPE_ERROR);
        }
        // 验证状态
        if (ObjectUtils.isEmpty(reqDTO.getState()) || UmsResourceStateEnum.noCode(reqDTO.getState())) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_STATE_ERROR);
        }
    }

    private void validateUmsResourceSaveReqDTO(UmsResourceSaveReqDTO reqDTO) {
        // 资源是否重复
        UmsResource resource = this.getResource(reqDTO.getCode());
        if (ObjectUtils.isNotEmpty(resource)) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_EXISTED);
        }
        // 验证类型
        if (ObjectUtils.isEmpty(reqDTO.getType()) || UmsResourceTypeEnum.noCode(reqDTO.getType())) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_TYPE_ERROR);
        }
        // 验证状态
        if (ObjectUtils.isEmpty(reqDTO.getState()) || UmsResourceStateEnum.noCode(reqDTO.getState())) {
            throw new BaseException(CodeMsg.UMS_RESOURCE_STATE_ERROR);
        }
    }
}
